package com.xiaomage.view.draw3

import android.content.Context
import android.graphics.*
import android.view.View

/**
 * Created by Administrator on 2018/1/2.
 */
class RangeView2(context: Context): View(context) {
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //定义画笔
        val paint= Paint()
        paint.isAntiAlias=true
        paint.style= Paint.Style.STROKE
        paint.strokeWidth=5f
        paint.color= Color.parseColor("#448ced")

        //构造两个矩形
        val rect1 = Rect(100, 100, 400, 200)
        val rect2 = Rect(200, 0, 300, 300)
        canvas.drawRect(rect1,paint)
        canvas.drawRect(rect2,paint)
        //两个区域的交集
        //定义画笔
        val paint2= Paint()
        paint2.isAntiAlias=true
        paint2.style= Paint.Style.FILL
        paint2.strokeWidth=5f
        paint2.color= Color.parseColor("#fc4c4f")
        val region1=Region(rect1)
        val region2=Region(rect2)
        region1.op(region2, Region.Op.INTERSECT)
        drawRegion(canvas,region1,paint2)

    }
    fun drawRegion(canvas: Canvas, region: Region, paint: Paint){
        val regionIterator= RegionIterator(region)
        val rect= Rect()
        while (regionIterator.next(rect)){
            canvas.drawRect(rect,paint)
        }

    }
}